export default {
    state: {
        menuData: JSON.parse(sessionStorage.getItem('menuData')) || [],
        activeMenuIndex: JSON.parse(sessionStorage.getItem('activeMenuIndex')) || "1",
        opendsKey: JSON.parse(sessionStorage.getItem('opendsKey')) || [],
        activeMenuItem: JSON.parse(sessionStorage.getItem('activeMenuItem')) || {},
        tags: JSON.parse(sessionStorage.getItem('tags')) || [],
    },
    mutations: {
        setActiveMenuItem(state, log) {
            sessionStorage.setItem(`activeMenuItem`, JSON.stringify(log));
            state.activeMenuItem = log;
        },
        setMenuData(state, log) {
            sessionStorage.setItem(`menuData`, JSON.stringify(log));
            state.menuData = log;
        },
        setActiveMenuIndex(state, log) {
            sessionStorage.setItem(`activeMenuIndex`, JSON.stringify(log));
            state.activeMenuIndex = log;
        },
        setOpendsKey(state, log) {
            sessionStorage.setItem(`opendsKey`, JSON.stringify(log));
            state.opendsKey = log;
        },
        setTags(state, log) {
            const arr = state.tags;
            if (arr.length === 0) {
                arr.push(log)
            } else {
                console.log(log.name)
                const n = arr.findIndex(ele => {
                    return log.name == ele.name
                })
                if (n === -1) {
                    arr.push(log);
                    sessionStorage.setItem(`tags`, JSON.stringify(arr));
                    state.tags = arr;
                }
            }
        },
        spliceOpendsKey(state, log) {
            const arr = state.opendsKey;
            const n = arr.findIndex(ele => {
                return log === ele
            })
            if (n != -1) {
                arr.splice(n, 1)
                sessionStorage.setItem(`opendsKey`, JSON.stringify(arr));
                state.opendsKey = arr;
            }

        },
        removeTags(state, log) {
            const arr = state.tags;
            const n = arr.findIndex((ele) => {
                return ele.name == log.name;
            });
            if (n != -1) {
                arr.splice(n, 1);
                sessionStorage.setItem(`tags`, JSON.stringify(arr));
                state.tags = arr;
            }

        },
        clearTags(state) {
            state.tags = []
        }
    },
    actions: {
        setTags({ commit }, log) {
            commit("setTags", log);
        },
        setActiveMenuItem({ commit }, log) {
            commit("setActiveMenuItem", log);
        },
        setMenuData({ commit }, log) {
            commit("setMenuData", log);
        },
        setActiveMenuIndex({ commit }, log) {
            commit("setActiveMenuIndex", log);
        },
        setOpendsKey({ commit }, log) {
            commit("setOpendsKey", log);
        },
        spliceOpendsKey({ commit }, log) {
            commit("spliceOpendsKey", log);
        },
        removeTags({ commit }, log) {
            commit("removeTags", log);
        },
        clearTags({ commit }) {
            commit("clearTags");
        }
    },
    getters: {
        getTags(state) {
            return state.tags;
        },
        getActiveMenuItem(state) {
            return state.activeMenuItem;
        },
        getMenuData(state) {
            return state.menuData;
        },
        getActiveMenuIndex(state) {
            return state.activeMenuIndex;
        },
        getOpendsKey(state) {
            return state.opendsKey;
        },
    }
}
